﻿using AutoMapper;
using PeiYue.Common.Helper;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Tms;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Tms
{
    public class VehicleServices : BaseServices<VehicleDao>, IVehicleServices
    {
        IBaseRepository<VehicleDao> _dal;
        IMapper _mapper;
        public VehicleServices(IBaseRepository<VehicleDao> dal, IMapper mapper)
        {
            this._dal = dal;
            base.BaseDal = dal;
            this._mapper = mapper;
        }
        /// <summary>
        /// 获取车辆分页列表
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        public async Task<PageModel<SearchVehicleResponseDto>> GetPage(SearchVehicleRequestDto requestDto)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select v.*,u1.namec as create_names,u2.namec as update_names,r.names as router_name ");
            strSql.Append("from oms_wms_vehicle v ");
            strSql.Append("left join oms_ur_user u1 on v.create_user=u1.id ");
            strSql.Append("left join oms_ur_user u2 on v.update_user=u2.id ");
            strSql.Append("left join oms_wms_router r on r.id=v.rt_id ");
            strSql.Append("where 1=1 ");
            List<SugarParameter> parameters = new List<SugarParameter>();
            if (requestDto.router_id.HasValue && requestDto.router_id > 0)
            {
                strSql.Append("and v.rt_id=@rt_id ");
                parameters.Add(new SugarParameter("@rt_id", requestDto.router_id));
            }
            
            if (requestDto.status.HasValue&&requestDto.status > 0)
            {
                strSql.Append("and v.status=@status ");
                parameters.Add(new SugarParameter("@status", requestDto.status));
            }
            else
            {
                strSql.Append("and v.status<3 ");
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timef))
            {
                strSql.Append("and v.create_time>=@create_time_begin ");
                parameters.Add(new SugarParameter("@create_time_begin", requestDto.timef));
            }
            if (!string.IsNullOrWhiteSpace(requestDto.timet))
            {
                strSql.Append("and v.create_time<@create_time_end ");
                parameters.Add(new SugarParameter("@create_time_end", Convert.ToDateTime(requestDto.timet).AddDays(1)));
            }
            return await base.SqlQueryPage<SearchVehicleResponseDto>(strSql.ToString(), parameters, requestDto.pageIndex, requestDto.pageSize, " v.id desc ");
        }
        /// <summary>
        /// 获取车辆详情
        /// </summary>
        /// <param name="id">车辆id</param>
        /// <returns></returns>
        public async Task<SearchVehicleResponseDto> GetById(long id)
        {
            var model = await base.QueryById(id);
            SearchVehicleResponseDto responseDto = _mapper.Map<SearchVehicleResponseDto>(model);
            return responseDto;
        }
    }
}
